package Thread;

/**
 * @author 王艺锡
 * @version 1.0
 * 通过实现Runnable接口来创建进程
 */
//1.java是单继承的，有时候一个类已经继承了别的类，此时想要通过继承Thread类来创建线程显然行不通，
// 所以可以通过实现Runnable接口来创建线程
public class Thread02 {
    public static void main(String[] args) {
        Dog dog = new Dog();
        //dog.start();//err 这里不能调用start

        //dog类没有继承Thread类，不能使用start方法
        //所以通过Thread类，让Thread代理调用start方法
        Thread thread = new Thread(dog);
        thread.start();

        //模拟Thread是如何代理开辟线程
//        Tiger tiger = new Tiger();
//        ThreadProxy threadProxy = new ThreadProxy(tiger);
//        threadProxy.start();
    }
}

class Dog implements Runnable {
    int count = 0;

    @Override
    public void run() {
        while (count < 10) {
            System.out.println("好久不见.." + ++count + Thread.currentThread().getName());
            //休眠1s
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

//模拟Thread是如何代理开辟线程
class ThreadProxy implements Runnable {
    private Runnable target;

    public ThreadProxy(Runnable target) {
        this.target = target;
    }

    @Override
    public void run() {
        if (target != null) {
            target.run();
        }
    }

    public void start(){
        start0();
    }

    public void start0(){
        run();
    }
}

class Animal{}
class Tiger extends Animal implements Runnable{
    @Override
    public void run() {
        System.out.println("虎啸山林..");
    }
}
